package cn.ncu.wechat.service.impl;

import cn.ncu.wechat.util.ConfigCheckUtil;
import cn.ncu.wechat.util.Result;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ncu.wechat.model.MiniappConfigDraft;
import cn.ncu.wechat.dao.MiniappConfigDraftMapper;
import cn.ncu.wechat.service.MiniappConfigDraftService;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* @author 12638
* @description 针对表【miniapp_config_draft(小程序配置草稿表)】的数据库操作Service实现
* @createDate 2025-02-06 21:16:04
*/
@Service
public class MiniappConfigDraftServiceImpl extends ServiceImpl<MiniappConfigDraftMapper, MiniappConfigDraft>
    implements MiniappConfigDraftService{


    @Override
    public Result<?> saveMiniAppConfig(Map<String, Object> request) {
        try {
            String request_check = ConfigCheckUtil.validateConfig(request);
            if ("配置有效".equals(request_check)) {
                Gson gson = new Gson();
                MiniappConfigDraft miniappConfigDraft = getById(1);
                miniappConfigDraft.setConfigInfo(gson.toJson(request));
                boolean succ = updateById(miniappConfigDraft);
                if (!succ){
                    return Result.error(500,"未保存成功");
                }
                return Result.success();
            } else {
                return Result.error(400, request_check);
            }

        } catch (Exception e) {
            return Result.error(500, "保存失败");
        }
    }
}




